using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2004-08-10
	Status:		dev
	Version:	0.0.1
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.workflowX {
	/// <summary>
	/// This class gives access to a single work-status or list of work-statuses defined in the datastore.
	/// <para>A work-status defines a condition for the work-item, typically this indicates the priority status of the work-item.</para>
	/// </summary>
	public class x_workstatus : x_DbData {
		#region Constants
		private const string workstatusxml = "<workflowX><workstatus/></workflowX>";
		private const string error_get 			= "The status does not exist within the system.";
		private const string error_nullid		= "The status identifier is null.";
		#endregion

		#region Invisible properties
		private XmlDocument workstatus;
		#endregion

		#region Visible properties
		/// <summary>A work-status definiton</summary>
		/// <value>The work-status definiton rendered as an Xml Element</value>
		public XmlElement WorkStatus {
			get { return (XmlElement) workstatus.SelectSingleNode("/workflowX/workstatus"); }
		}
		/// <summary>A work-status collection</summary>
		/// <value>The collection of work-statuses rendered as an Xml Element</value>
		public XmlElement Statuses {
			get { 
				getStatuses();
				return (XmlElement) workstatus.SelectSingleNode("/workflowX/workstatus/workstatuses");
			}
		}
		private int id;
		/// <summary>The work-status identifier</summary>
		/// <value>The work-status identifier within the datastore</value>
		public int ID {
			get { return id; }
			set { 
				id = value;
				WorkStatus.SetAttribute("id", id.ToString());
			}
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>work-status constructor</overloads>
		/// <summary>Initialises the datastore connection string</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		public x_workstatus(string DSN) : base(DSN) {
			initialise();
		}
		/// <summary>Retrieves the work-status from the datastore</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		/// <param name="StatusID">The work-status identifier</param>
		/// <exception cref="x_exception">Cannot find a work-status for the given identifier</exception>
		public x_workstatus(string DSN, int StatusID) : base(DSN) {
			initialise();
			Get(StatusID);
		}
		#endregion

		#region Public methods
		///	<summary>
		///	Creates a work-status and saves it into the database
		///	</summary>
		/// <param name="Name">The name of the work-status</param>
		/// <param name="Description">The description of the work-status</param>
		public void Create(string Name, string Description) {
			_getCommand("xStatusAdd", CommandType.StoredProcedure);
			_addDbParameter(Name, "@Name", 50);
			_addDbParameter(Description, "@Description", 200);
			IDataParameter parID = _addDbParameter("@ID");
			_executeNonQuery();
			ID = (int) _getParameter(parID);
		}
		/// <overloads>Retreives a work-status from the database</overloads>
		///	<summary>
		///	The work-status is identified
		///	</summary>
		/// <param name="StatusID">The work-status identifier</param>
		/// <exception cref="x_exception">Cannot find a work-status for the given identifier</exception>
		public void Get(int StatusID) {
			ID = StatusID;
			Get();
		}
		///	<summary>
		///	Assumes the work-status has been identified via the ID property
		///	</summary>
		/// <exception cref="x_exception">The work-status identifier is null, or Cannot find a work-status for the given identifier</exception>
		public void Get() {
			if (id == 0)
				throw(new x_exception("error_nullid", error_nullid));
			else
				getStatus();
		}
		private void getStatus() {
			_getCommand("xStatusGet", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			if(rdr.Read()) {
				_addAttribute(workstatus, WorkStatus, "name", _getString(rdr, "Name"));
				_addElement(workstatus, WorkStatus, "description", _getString(rdr, "Description"));
			} else {
				throw(new x_exception("error_get", error_get));
			}
			rdr.Close();
		}
		private void getStatuses() {
			_getCommand("xStatusGet", CommandType.StoredProcedure);
			_addDbParameter(0, "@ID");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			XmlElement statuses = (XmlElement) WorkStatus.AppendChild(workstatus.CreateElement("workstatuses"));
			while(rdr.Read()) {
				XmlElement status = (XmlElement) statuses.AppendChild(workstatus.CreateElement("workstatus"));
				_addAttribute(workstatus, status, "id", _getString(rdr, "ID"));
				_addAttribute(workstatus, status, "name", _getString(rdr, "Name"));
				_addElement(workstatus, status, "description", _getString(rdr, "Description"));
			}
			rdr.Close();
		}
		///	<summary>
		///	Saves an existing work-status to the database
		///	</summary>
		/// <param name="StatusID">The work-status identifier</param>
		/// <param name="Name">The name of the work-status</param>
		/// <param name="Description">The description of the work-status</param>
		public void Save(int StatusID, string Name, string Description) {
			ID = StatusID;
			_getCommand("xStatusSave", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			_addDbParameter(Name, "@Name", 50);
			_addDbParameter(Description, "@Description", 200);
			_executeNonQuery();
		}
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		private void initialise() {
			workstatus = new XmlDocument();
			workstatus.LoadXml(workstatusxml);
		}
		#endregion

	}
}
